package com.tarena.lbs.attach.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 文件上传工具类
 */
@Component
public class UploadUtil {
    public static String upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        assert filename != null;
        String suffix = filename.substring(filename.lastIndexOf("."));// 获取文件后缀名
        filename = UUID.randomUUID() + suffix;

        String drl = "d:/files";
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());


        File filePath = new File(drl + datePath);
        if (!filePath.exists()){
            filePath.mkdirs();
        }
        file.transferTo(new File(drl + datePath + filename));

        return datePath+ filename;
    }
}
